package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.tfjybj.itoo.exam.provider.service.RoomService;
import com.tfjybj.itoo.teach.FacadeApi.RoomApi;
import com.tfjybj.itoo.teach.model.RoomModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * RoomController
 * room表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Api(tags = {"room表接口"})
@RequestMapping(value = "/room")
@RestController
@Slf4j
public class RoomController {

    @Resource
    private RoomService roomService;

    @Reference(check = false)
    private RoomApi roomApi;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model RoomModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody RoomModel model) {
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getRoomCapacity())) {
            return ItooResult.build(ItooResult.FAIL, "roomCapacity为空");
        }
        return roomApi.create(model);
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        if (StringUtils.isEmpty(id)){
            return ItooResult.build(ItooResult.FAIL, "id为空");
        }
        return roomApi.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        return roomApi.deleteByIds(ids);
    }

    /**
     * 修改
     *
     * @param model RoomModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id修改room")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody RoomModel model) {
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getRoomCapacity())) {
            return ItooResult.build(ItooResult.FAIL, "roomCapacity为空");
        }
        return roomApi.modify(model);
    }

    /**
     * 根据id查找Room
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        if (StringUtils.isEmpty(id)){
            return ItooResult.build(ItooResult.FAIL, "id为空");
        }
        return roomApi.findById(id);
    }

    /**
     * 分页查询所有Room
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "分页查询所有Room")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        return roomApi.queryPageAll(pageNo, pageSize);
    }

    /**
     * 根据房间代码模糊查询room
     *
     * @param code     房间代码
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的room
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据房间代码模糊查询room", notes = "分页根据房间代码模糊查询room")
    @GetMapping(value = "queryRoomByLikeCode/{pageNo}/{pageSize}")
    public ItooResult queryRoomByLikeCode(@RequestParam(required = false, defaultValue = "") String code,
                                          @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                          @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        return roomApi.queryRoomByLikeCode(code, pageNo, pageSize);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据building_id查找所有房屋
     *
     * @param buildingId buildingId
     * @author 杨江涛
     * @since 2.0.0 2018-11-21 21:35:56
     */
    @ApiOperation(value = "buildingId查找所有房屋")
    @GetMapping(value = {"/findByBuildingId/{buildingId}"})
    public ItooResult findByBuildingId(@ApiParam(value = "buildingId", required = true) @PathVariable String buildingId) {
        if (StringUtils.isEmpty(buildingId)) {
            return ItooResult.build(ItooResult.FAIL, "建筑ID为空");
        }
        return roomService.findByBuildingId(buildingId);
    }

    /**
     * 根据roomId查找Room表所有信息
     *
     * @param roomId 主键roomId
     * @return roomEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据roomId查找Room表所有信息（返回list）")
    @GetMapping(value = {"/findByRoomId/{roomId}"})
    public ItooResult findByRoomId(@ApiParam(value = "主键roomId", required = true) @PathVariable String roomId) {
        if (StringUtils.isEmpty(roomId)){
            return ItooResult.build(ItooResult.FAIL, "主键roomId为空");
        }
        return roomService.findByRoomId(roomId);
    }

    /**
     * 根据校区代码查找Room表所有校区所有建筑的所有机房（返回list）
     *
     * @param schoolBranch 地理位置(010_西校区，011_东校区)
     * @return roomEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-30 16:47:29
     */
    @ApiOperation(value = "根据校区值查找Room表所有校区所有建筑的所有机房（返回list,地理位置(010_西校区，011_东校区)）")
    @GetMapping(value = {"/findComputerRoom/{schoolBranch}"})
    public ItooResult findComputerRoom(@ApiParam(value = "校区schoolBranch", required = true) @PathVariable String schoolBranch) {
        if (StringUtils.isEmpty(schoolBranch)){
            return ItooResult.build(ItooResult.FAIL, "校区schoolBranch为空");
        }
        return roomService.findComputerRoom(schoolBranch);
    }




    /**
     * 根据roomId查找Room表所有信息
     *
     * @param pageNo     页码
     * @param pageSize   每页条数
     * @param searchInfo 查询参数
     * @return roomEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-12-6 19:17:44
     */
    @ApiOperation(value = "根据roomId或roomName模糊查找Room表所有信息（返回list）", notes = "根据roomId或roomName模糊查找Room表所有信息")
    @GetMapping(value = "findRoom/{searchInfo}/{pageNo}/{pageSize}")
    public ItooResult findRoom(
            @ApiParam(value = "房屋查询参数", required = true) @PathVariable String searchInfo,
            @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        return roomService.findRoom(searchInfo, pageNo, pageSize);
    }


    /**
     * 根据校区号和开考时间筛选教师
     *
     * @param schoolCode 校区号
     * @param startTime  配置考试开始时间
     * @author 马莹
     * @since 2018-12-7 20:53:31
     */
    @ApiOperation(value = "根据校区值查找Room表所有校区所有建筑的所有机房（返回list,地理位置(010_西校区，011_东校区)）")
    @GetMapping(value = {"/queryNoExamRoom/{schoolCode}/{startTime}"})
    public ItooResult queryNoExamRoom(@ApiParam(value = "校区schoolBranch", required = true) @PathVariable String schoolCode, @PathVariable String startTime) {
        return roomService.queryNoExamRoom(schoolCode, startTime);
    }

    /**
     * 根据文本框中的值,模糊查询
     *
     * @param fuzzyValue 文本框中的值
     * @param schoolKey  校区号
     * @param startTime  考试开始时间
     * @param endTime    考试结束时间
     * @return 筛选的模糊值
     * @author 马莹
     * @since 2019-5-5 20:39:55
     */
    @ApiOperation(value = "根据搜索框模糊查询考场")
    @GetMapping(value = "/selNotExamRoom/{fuzzyValue}/{schoolKey}/{startTime}/{endTime}")

    public ItooResult selNotExamRoom(@ApiParam(value = "考场name", required = true) @PathVariable String fuzzyValue,
                                     @ApiParam(value = "校区key", required = true) @PathVariable String schoolKey,
                                     @ApiParam(value = "考试开始时间", required = true) @PathVariable String startTime, @PathVariable String endTime) {
        List<com.tfjybj.itoo.exam.model.RoomModel> roomModelList = roomService.selNotExamRoom(fuzzyValue, schoolKey, startTime, endTime);
        if (roomModelList.size() > 0) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomModelList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "没有符合条件的考场");
        }
    }

    /**
     * 根据东西校区以及考试开始时间加载考场
     *
     * @param schoolKey 校区key
     * @param startTime 考试开始时间
     * @param endTime   考试结束时间
     * @return 考场集合
     * @since 2019-3-24 17:33:12
     */
    @ApiOperation(value = "根据东西校区查询考场")
    @GetMapping(value = "/selNotRoom/{schoolKey}/{startTime}/{endTime}")
    public ItooResult selNotRoom(@ApiParam(value = "校区key", required = true) @PathVariable String schoolKey,
                                 @ApiParam(value = "考试开始时间", required = true) @PathVariable String startTime, @PathVariable String endTime
    ) {
        List<com.tfjybj.itoo.exam.model.RoomModel> roomModelList = roomService.selNotRoom(schoolKey, startTime, endTime);
        if (roomModelList.size() > 0) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomModelList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "没有符合条件的考场");
        }
    }
}

